
#ifndef TESTBASE_H_
#define TESTBASE_H_
#include <unistd.h>
#include <cstring>
#include "../parser/ShellParser.h"
#include "../parser/Parser.h"
#include "../utils/Functions.h"
#include "../index/Index.h"
#include "../compresores/CodigoDelta.h"
#include <ctype.h>
#include <iostream>
#include "../utils/Merge/NWayMerge.h"
#include <time.h>
#include "../parser/DynamicShellParser.h"
#include <iomanip>
#include <bitset>
#include "../query/Query.h"

using std::string;
using std::cerr;
using std::cout;
using std::endl;
using std::stringstream;
using std::hex;
using namespace convertTypes;

class TestBase {
private:
	void testShellParser(int, char**);
	void testParser();
	void testUnitDocument();
	void testUtils();
	void testIndex();
	void testParser_UnitDocument_Merge_Index();
	void testWideCharacters();
	void testCodigosDelta();
	void testBitSet();
	void testParserDinYCache();
	void readOneWordFromFC();
	void testPointersInHexa();
	void testGetAllWordPointersToUInt();
	void testBinarySearchOfTokens();

// funciones para probar el merge
	void crearArchsAMergear();
	void crearArch11();
	void crearArch4();
	void crearArch9();
	void crearArch5();
	void crearArch2();
	void cargarArch11();
	void cargarArch4();
	void cargarArch9();
	void cargarArch5();
	void cargarArch2();
	void cargarArchsAMergear();
	void cerrarYLiberarArchs();


public:
	void ejecutarPruebaMerge();
	void runTest();
	void runProgram(int, char**);
	TestBase();
	~TestBase();
	std::fstream *fd11,*fd4,*fd9,*fd5,*fd2;
};

void TestBase::runTest() {
	try {
		testParser_UnitDocument_Merge_Index();
//		testBinarySearchOfTokens();
//		readOneWordFromFC();
//		testGetAllWordPointersToUInt();
//		testPointersInHexa();
//		testParserDinYCache();
//		testParser();
//		testWideCharacters();
//		testUnitDocument();
//		testUtils();
//		testIndex();
//		testCodigosDelta();
//		testBitSet();
//		ejecutarPruebaMerge();
	cout << " ------------------------ Terminó test --------------------------" << endl;
	} catch (std::exception &e) {
		cerr << " ------------------------ Falló test --------------------------" << endl;
	}
}


void TestBase::runProgram(int argc, char** argv) {
	try {
		testShellParser(argc, argv);

		cout << " ------------------------ Terminó test --------------------------" << endl;
	} catch (std::exception &e) {
		cerr << " ------------------------ Falló test --------------------------" << endl;
	}
}

void TestBase::testBinarySearchOfTokens() {
	string arch = string("./src/IndiceFinal/");
	arch += NOMBRE_ARCH_MAT_INICIAL;
	Query* q = new Query(arch);
	string token = string("transvestida");
	vector<Data>* v_data = q->busquedaBinaria(token);

	cout << "Documentos del termino: " << token << endl;
	for(unsigned int i=0; i<v_data->size(); i++) {
		cout << "\tdoc: " << v_data->at(i).document << " frec: " << v_data->at(i).frecuency << " posiciones:";
		for(unsigned int j=0; j<v_data->at(i).pos->size(); j++){
			cout << " " << v_data->at(i).pos->at(j);
		}
		cout << endl;
	}
	delete v_data;
	delete q;
}

void TestBase::testGetAllWordPointersToUInt() {

	ifstream* pointers = new ifstream (DOC_WORDS_POINTERS, ios::in | ios::binary);

	if (pointers->is_open()) {
		while(!pointers->eof()) {

			char * read = new char [4];
			pointers->read(reinterpret_cast<char*>(read), 4);

			if (!pointers->eof())
				cout << charToInt(read) << endl;
			delete read;
		}
	} else {
		cout << "No se pudo abrir para leer" << endl;
	}
	delete pointers;

}

void TestBase::testPointersInHexa() {

	unsigned int a = 24;

	stringstream sstr;
	sstr.str(string(""));

	ofstream* pointersFile = new ofstream (DOC_WORDS_POINTERS, ios::out | ios::binary | ios::trunc);

	// guardo el int como hexa en el stringstream
	sstr 	<< 	std::setfill ('0') << std::setw(sizeof(int)*2)
			<< hex << a;

	string bits = hexaToBits(sstr.str());
	pointersFile->write(bits.c_str(), sizeof(int));

	sstr.str(string("")); // vacio el stream asi porque el hexPointer.clear no funciona
	pointersFile->flush();
	pointersFile->close();

	ifstream* pointers = new ifstream (DOC_WORDS_POINTERS, ios::in | ios::binary);

    char * read = new char [4];
	string readed = string("");
	if (pointers->is_open()) {
		pointers->read(reinterpret_cast<char*>(read), 4);
		unsigned int number = hexaToUInt(read);
		cout << "Numero guardado: " << a << " Numero levantado: " << number << endl;
	} else {
		cout << "No se pudo abrir para leer" << endl;
	}
	delete pointers;
	delete pointersFile;
}

void TestBase::readOneWordFromFC() {
/*	ifstream* FCFile = new ifstream (DOC_F_C, ios::in | ios::binary);
	this->indexFile = new ifstream (indexFile.c_str(), ios::in | ios::binary);

	unsigned long int wordsNumber;
	this->indexFile->read (reinterpret_cast <char*> (&wordsNumber), sizeof (unsigned long int));
	*/
}

void TestBase::testShellParser(int argc, char **argv) {
	ShellParser * shellParser = new ShellParser();
	if (shellParser->getOptions(argc, argv) != EXIT_SUCCESS) {
		delete shellParser;
		exit(1);
	}
	delete shellParser;
}

void TestBase::testParser_UnitDocument_Merge_Index() {
	clock_t chrono = clock();
	clock_t clocks_per_sec = 1000000l;

	Parser* p = new Parser();

	char actualDir[256];

//	char* ptr = getcwd(actualDir, 255);
    strcat(actualDir, "/");

	printf("Directorio actual:\n\n%s\n\n", actualDir);
	p->parseDirectory(actualDir,"/home/ramiro/workspace7506/datos2013/src/");
	char dir[ANCHO_DIRECTORIO_ARCH_IDX_REPO] = CARPETA_ARCHS_A_MERGEAR;


	clock_t chrono1 = clock();
	if(SHOW_TIME > 0)
		cout << "Parser total time: " << ((float)chrono1-(float)chrono)/ clocks_per_sec << " s." <<endl;
	chrono = chrono1;


	printf("\t\t\t\t\tTERMINE DE PARSEAR EL DIRECTORIO\n");
	delete p;
//	dir = strcat(dir, CARPETA_ARCHS_A_MERGEAR);
	NWayMerge* merge = new NWayMerge(dir);
	merge->mergeDeNVias();
	cout << "--------------- terminos distintos: " << merge->getCantTerminoDistintos() << endl;
	delete merge;
	chrono1 = clock();
	if(SHOW_TIME > 0)
		cout << "Merge time: " << ((float)chrono1-(float)chrono)/ clocks_per_sec << " s." << endl;
	chrono = chrono1;

	unsigned int wordsNumber, i;
	string file = LSI_DIRECTORY;
	file.append(NOMBRE_ARCH_MAT_INICIAL);
	if (DEBUG > 0)
		cout << "Nombre del archivo a levantar el indice completo: " << file << endl;

	string rutaCarpetaIdx(RUTA_CARPETA_IDX_PRUEBA);
	Index* index = new Index(file,rutaCarpetaIdx);
	wordsNumber = index->getWordsNumber();

	unsigned int bloques = floor(wordsNumber/floor(log2(wordsNumber)));
	chrono1 = clock();
	for (i=0; i<bloques; i++) {
		index->readRegisters();
		index->frontCoding();
//		index->printRegisters();
	}
	delete index;
	chrono1 = clock();
	if(SHOW_TIME > 0)
		cout << "Index time: " << ((float)chrono1-(float)chrono)/ clocks_per_sec << " s." << endl;
}

void TestBase::testParser() {
	Parser* p = new Parser();
    char actualDir[256];

//    char* ptr = getcwd(actualDir, 255);
    strcat(actualDir, "/");

    printf("\t\t\t\t%s\n", actualDir);
	p->parseDirectory(actualDir,"/home/ramiro/workspace7506/datos2013/src/");
	char dir[ANCHO_DIRECTORIO_ARCH_IDX_REPO] = CARPETA_ARCHS_A_MERGEAR;

//	dir = strcat(dir, CARPETA_ARCHS_A_MERGEAR);
	NWayMerge* merge = new NWayMerge(dir);
	merge->mergeDeNVias();
	delete merge;
	delete p;
}

void TestBase::testUnitDocument() {

	string s1 = "hola";
	string s2 = "perro";
	string s3 = "gato";
	string s4 = "si";
	string s5 = "nadie";
	string s6 = "culo";
	string s7 = "javi";
	string s8 = "rama";
	string s9 = "fer";
	string s10 = "lea";
	string s11 = "gays";
	string s12 = "soporte";
	string s13 = "aplicaciones";
	string s14 = "tampoco";
	string s15 = "otorrinonaringologo";
	string s16 = "pedo";
	string s17 = "seeee";

	if (DEBUG > 1)
		printf("\nCreando doc...");
	UnitDocument* myDoc = new UnitDocument();
	if (DEBUG > 1)
		printf("OK\nAgregando terminos...");
	myDoc->setDocN(1);
	myDoc->addTermino(s1, 2);
	myDoc->addTermino(s2, 43);
	myDoc->addTermino(s17, 12);
	myDoc->addTermino(s15, 0);
	myDoc->addTermino(s3, 87);
	myDoc->addTermino(s9, 92);
	myDoc->addTermino(s7, 45);
	myDoc->addTermino(s13, 67);
	myDoc->addTermino(s5, 27);
	myDoc->addTermino(s11, 123);
	myDoc->addTermino(s8, 41);
	myDoc->addTermino(s2, 200);
	myDoc->addTermino(s15, 20);
	myDoc->addTermino(s1, 57);
	myDoc->addTermino(s10, 55);
	myDoc->addTermino(s16, 88);
	myDoc->addTermino(s12, 89);
	myDoc->addTermino(s1, 198);
	myDoc->addTermino(s3, 634);
	myDoc->addTermino(s4, 678);
	myDoc->addTermino(s6, 835);
	myDoc->addTermino(s14, 245);
	myDoc->addTermino(s3, 10);
	myDoc->addTermino(s14, 32);
	myDoc->addTermino(s16, 66);
	myDoc->addTermino(s11, 100);
	myDoc->addTermino(s10, 152);
	myDoc->addTermino(s11, 39);
	myDoc->addTermino(s3, 91);
	myDoc->addTermino(s5, 92);
	myDoc->addTermino(s9, 93);
	myDoc->addTermino(s17, 342);
	myDoc->addTermino(s1, 222);
	myDoc->addTermino(s2, 277);
	myDoc->addTermino(s10, 552);
	if (DEBUG > 1)
		printf("OK\n");

	unsigned int t = myDoc->getTerminos()->size();
	if (DEBUG > 1)
		printf("Cantidad de terminos agregados (35): %d\n", t);

	myDoc->sort();
	myDoc->stdOutDoc();
}

void TestBase::testUtils() {
	int i = 4;
	cout << "numero 4: "<< intToString(i) << endl;
	int asdas = strToint("54");

	cout << "numero: " << asdas << endl;
}

void TestBase::testWideCharacters() {
	wchar_t wc = L'Á';
	std::wcout << " Pruebo el numero: " << (int)wc << " como char: " << wc << endl;

	wc = L'É';
	std::wcout << " Pruebo el numero: " << (int)wc << " como char: " << wc << endl;

	wc = L'Í';
	std::wcout << " Pruebo el numero: " << (int)wc << " como char: " << wc << endl;

	wc = L'Ó';
	std::wcout << " Pruebo el numero: " << (int)wc << " como char: " << wc << endl;

	wc = L'Ú';
	std::wcout << " Pruebo el numero: " << (int)wc << " como char: " << wc << endl;

	wc = L'Ñ';
	std::wcout << " Pruebo el numero: " << (int)wc << " como char: " << wc << endl;

}

inline TestBase::TestBase() {
}

inline TestBase::~TestBase() {
}

void TestBase::testIndex() {
	unsigned int wordsNumber, i;
	string file = LSI_DIRECTORY;
	file.append(NOMBRE_ARCH_MAT_INICIAL);
	if (DEBUG > 0)
		cout << "Nombre del archivo a levantar el indice completo: " << file << endl;
	ofstream arc (file.c_str(), ios::out | ios::binary);
	int a = 4;
	int b = 2;
	int c = 14;
	int d = 1;
	arc.write(reinterpret_cast <char*> (&a), sizeof(a));

	arc.write(reinterpret_cast <char*> (&b), sizeof(b));
	string p1 = "casa";
	short int t = p1.size();
	arc.write(reinterpret_cast <char*> (&t), 2);
	arc.write(p1.c_str(), t);
	arc.write(reinterpret_cast <char*> (&a), sizeof(int));
	arc.write(reinterpret_cast <char*> (&b), sizeof(int));
	arc.write(reinterpret_cast <char*> (&c), sizeof(int));
	arc.write(reinterpret_cast <char*> (&c), sizeof(int));
	arc.write(reinterpret_cast <char*> (&a), sizeof(int));
	arc.write(reinterpret_cast <char*> (&d), sizeof(int));
	arc.write(reinterpret_cast <char*> (&c), sizeof(int));
	arc.write(reinterpret_cast <char*> (&b), sizeof(int));
	p1 = "casadores";
	t = p1.size();
	arc.write(reinterpret_cast <char*> (&t), 2);
	arc.write(p1.c_str(), t);
	arc.write(reinterpret_cast <char*> (&a), sizeof(int));
	arc.write(reinterpret_cast <char*> (&b), sizeof(int));
	arc.write(reinterpret_cast <char*> (&c), sizeof(int));
	arc.write(reinterpret_cast <char*> (&c), sizeof(int));
	arc.write(reinterpret_cast <char*> (&a), sizeof(int));
	arc.write(reinterpret_cast <char*> (&d), sizeof(int));
	arc.write(reinterpret_cast <char*> (&c), sizeof(int));
	arc.write(reinterpret_cast <char*> (&b), sizeof(int));
	p1 = "casados";
	b = 2;
	c = 25;
	d = 1;

	t = p1.size();
	arc.write(reinterpret_cast <char*> (&t), 2);
	arc.write(p1.c_str(), t);
	arc.write(reinterpret_cast <char*> (&a), sizeof(int));
	arc.write(reinterpret_cast <char*> (&b), sizeof(int));
	arc.write(reinterpret_cast <char*> (&c), sizeof(int));
	arc.write(reinterpret_cast <char*> (&c), sizeof(int));
	arc.write(reinterpret_cast <char*> (&a), sizeof(int));
	arc.write(reinterpret_cast <char*> (&d), sizeof(int));
	arc.write(reinterpret_cast <char*> (&c), sizeof(int));

	p1 = "cazador";
	b = 2;
	c = 62;
	d = 1;

	t = p1.size();
	arc.write(reinterpret_cast <char*> (&b), sizeof(int));
	arc.write(reinterpret_cast <char*> (&t), 2);
	arc.write(p1.c_str(), t);
	arc.write(reinterpret_cast <char*> (&a), sizeof(int));
	arc.write(reinterpret_cast <char*> (&b), sizeof(int));
	arc.write(reinterpret_cast <char*> (&d), sizeof(int));
	arc.write(reinterpret_cast <char*> (&c), sizeof(int));
	arc.write(reinterpret_cast <char*> (&a), sizeof(int));
	arc.write(reinterpret_cast <char*> (&d), sizeof(int));
	arc.write(reinterpret_cast <char*> (&c), sizeof(int));

	/*
	arc.write("2", sizeof(int));
	arc.write("8", 2);
	arc.write("casadose", 8);
	arc.write("8", sizeof(int));
	arc.write("2", sizeof(int));
	arc.write("90", sizeof(int));
	arc.write("50", sizeof(int));
	arc.write("7", sizeof(int));
	arc.write("1", sizeof(int));
	arc.write("80", sizeof(int));
	arc.write("2", sizeof(int));
	arc.write("5", 2);
	arc.write("dedos", 5);
	arc.write("5", sizeof(int));
	arc.write("2", sizeof(int));
	arc.write("900", sizeof(int));
	arc.write("500", sizeof(int));
	arc.write("6", sizeof(int));
	arc.write("1", sizeof(int));
	arc.write("60", sizeof(int));
	arc.write("2", sizeof(int));
	arc.write("7", 2);
	arc.write("dedotes", 7);
	arc.write("8", sizeof(int));
	arc.write("2", sizeof(int));
	arc.write("90", sizeof(int));
	arc.write("50", sizeof(int));
	arc.write("7", sizeof(int));
	arc.write("1", sizeof(int));
	arc.write("80", sizeof(int));
	arc.write("2", sizeof(int));
	arc.write("9", 2);
	arc.write("dedoteses", 9);
	arc.write("5", sizeof(int));
	arc.write("2", sizeof(int));
	arc.write("900", sizeof(int));
	arc.write("500", sizeof(int));
	arc.write("6", sizeof(int));
	arc.write("1", sizeof(int));
	arc.write("60", sizeof(int));
	arc.write("2", sizeof(int));
	arc.write("8", 2);
	arc.write("devendra", 8);
	arc.write("8", sizeof(int));
	arc.write("2", sizeof(int));
	arc.write("90", sizeof(int));
	arc.write("50", sizeof(int));
	arc.write("7", sizeof(int));
	arc.write("1", sizeof(int));
	arc.write("80", sizeof(int));
	arc.write("2", sizeof(int));
	arc.write("6", 2);
	arc.write("espejo", 6);
	arc.write("5", sizeof(int));
	arc.write("2", sizeof(int));
	arc.write("900", sizeof(int));
	arc.write("500", sizeof(int));
	arc.write("6", sizeof(int));
	arc.write("1", sizeof(int));
	arc.write("60", sizeof(int));
	arc.write("2", sizeof(int));
	arc.write("8", 2);
	arc.write("espejos", 8);
	arc.write("8", sizeof(int));
	arc.write("2", sizeof(int));
	arc.write("90", sizeof(int));
	arc.write("50", sizeof(int));
	arc.write("7", sizeof(int));
	arc.write("1", sizeof(int));
	arc.write("80", sizeof(int)); */
	arc.close();


	string cadenaRutaCArpetaIdx(RUTA_CARPETA_IDX_PRUEBA);
	Index* index = new Index(file,cadenaRutaCArpetaIdx);
	wordsNumber = index->getWordsNumber();

	for (i=0; i<log2(wordsNumber); i++) {
		if (DEBUG > 1)
			printf("leo...\n");
		index->readRegisters();
		index->frontCoding();
		index->printRegisters();
	}

	delete index;
}

void TestBase::testCodigosDelta(){

/*	string unario = delta->getCodigoUnario(5);
	cout<<unario<<endl;//Resultado esperado 11110
	string gamma = delta->getCodigoGamma(5);
	cout<<gamma<<endl;//Resultado esperado 11001
	string delt = delta->getCodigoDelta(5);
	cout<<delt<<endl;//Resultado esperado 10101
*/

	CodigoDelta* delta = new CodigoDelta();
	ofstream* file = new ofstream ("pruebaDelta.dlt", ios::out | ios::binary);

	string a = "111111111110";
	unsigned int ultimoBit = 3;
	int b = delta->getNumeroFromCodigoUnario(a, &ultimoBit);
	cout<< b << endl;


	vector<Data>* data= new vector<Data>();

	for(unsigned int i=0; i<2; i++) {
		Data dat;
		dat.document = (unsigned int)31;

		dat.frecuency = (unsigned int)5;
		dat.pos = new vector<unsigned int>();
		dat.pos->push_back((unsigned int)1);
		dat.pos->push_back((unsigned int)2);
		dat.pos->push_back((unsigned int)3);
		dat.pos->push_back((unsigned int)4);
		dat.pos->push_back((unsigned int)9089);

		data->push_back(dat);
	}
	string vectorComprimido = delta->getCharArrayComprimido(data);
//	cout<<sizeof(vectorComprimido.c_str())<<endl;
	int uno = 1;
	file->write(reinterpret_cast<char*>(&uno), sizeof(int));
	file->write(vectorComprimido.c_str(), vectorComprimido.length());
	file->flush();
	file->close();
	cout << "el vector comprimido: " << vectorComprimido << endl;

	ifstream* fileD = new ifstream ("pruebaDelta.dlt", ios::out | ios::binary);
	vector<Data>* datas = delta->getDataFromFile(fileD);


	cout<< "imprimiendo..." << endl;
	for (unsigned int i=0; i<datas->size(); i++) {
		cout << "doc: "<< datas->at(i).document << endl;
		cout << "frec: "<< datas->at(i).frecuency << endl;
		for (unsigned int j=0; j<datas->at(i).pos->size(); j++) {
			cout << "pos: "<< datas->at(i).pos->at(j) << endl;
		}
	}
	cout << "-------Acabo TestDelta--------" << endl;
}

void TestBase::testBitSet(){
	bitset<8> bs = strToBitset("11011010");
	for (int i=0; i< 8; i++){
		cout<<bs[i]<<endl;
	}
}

void TestBase::testParserDinYCache(){
	// prueba la cache usando el dynamicParser
	DynamicShellParser *dynsp = new DynamicShellParser();
	dynsp->run();
	delete dynsp;
}

void TestBase::crearArch11(){

	char rutaDirAMergear[ANCHO_DIRECTORIO_ARCH_IDX_REPO];
    std::string nombreConcatenadoConRutaArch;

    strcpy(rutaDirAMergear,CARPETA_ARCHS_A_MERGEAR);
    nombreConcatenadoConRutaArch = strcat(rutaDirAMergear,"11.IDX");
    this->fd11 = new (std::nothrow)std::fstream(nombreConcatenadoConRutaArch.c_str(), std::ios::out);
    if(this->fd11 == NULL)
    	std::cerr << "Error al crear archivo de prueba 11.IDX" <<std::endl;
    else{
    	this->fd11->close();
    	this->fd11->open(nombreConcatenadoConRutaArch.c_str(),std::ios::binary | std::ios::in | std::ios::out);
    }
}

void TestBase::crearArch4(){

	char rutaDirAMergear[ANCHO_DIRECTORIO_ARCH_IDX_REPO];
    std::string nombreConcatenadoConRutaArch;

    strcpy(rutaDirAMergear,CARPETA_ARCHS_A_MERGEAR);
    nombreConcatenadoConRutaArch = strcat(rutaDirAMergear,"4.IDX");
    this->fd4 = new (std::nothrow)std::fstream(nombreConcatenadoConRutaArch.c_str(),std::ios::out);
    if(this->fd4 == NULL)
    	std::cerr << "Error al crear archivo de prueba 4.IDX" <<std::endl;
}

void TestBase::crearArch9(){

	char rutaDirAMergear[ANCHO_DIRECTORIO_ARCH_IDX_REPO];
    std::string nombreConcatenadoConRutaArch;

    strcpy(rutaDirAMergear,CARPETA_ARCHS_A_MERGEAR);
    nombreConcatenadoConRutaArch = strcat(rutaDirAMergear,"9.IDX");
    this->fd9 = new (std::nothrow)std::fstream(nombreConcatenadoConRutaArch.c_str(),std::ios::out);
    if(this->fd9 == NULL)
    	std::cerr << "Error al crear archivo de prueba 9.IDX" <<std::endl;
}

void TestBase::crearArch5(){

	char rutaDirAMergear[ANCHO_DIRECTORIO_ARCH_IDX_REPO];
    std::string nombreConcatenadoConRutaArch;

    strcpy(rutaDirAMergear,CARPETA_ARCHS_A_MERGEAR);
    nombreConcatenadoConRutaArch = strcat(rutaDirAMergear,"5.IDX");
    this->fd5 = new (std::nothrow)std::fstream(nombreConcatenadoConRutaArch.c_str(),std::ios::out);
    if(this->fd5 == NULL)
    	std::cerr << "Error al crear archivo de prueba 5.IDX" <<std::endl;
}

void TestBase::crearArch2(){

	char rutaDirAMergear[ANCHO_DIRECTORIO_ARCH_IDX_REPO];
    std::string nombreConcatenadoConRutaArch;

    strcpy(rutaDirAMergear,CARPETA_ARCHS_A_MERGEAR);
    nombreConcatenadoConRutaArch = strcat(rutaDirAMergear,"2.IDX");
    this->fd2 = new (std::nothrow)std::fstream(nombreConcatenadoConRutaArch.c_str(),std::ios::out);
    if(this->fd2 == NULL)
    	std::cerr << "Error al crear archivo de prueba 2.IDX" <<std::endl;
}

void TestBase::crearArchsAMergear(){

	this->crearArch11();
	this->crearArch4();
	this->crearArch9();
	this->crearArch5();
	this->crearArch2();
}

void TestBase::cargarArch11(){

	unsigned int nro_doc = 3;
	unsigned char largoterm = 1;
	unsigned int rep = 2;
	unsigned int pos1 = 110;
	unsigned int pos2 = 111;
	unsigned int pos3 = 112;

	fd11->write((char*)(&nro_doc),sizeof(int));
	fd11->write((char*)(&largoterm),1);
	fd11->write("a",largoterm);
	fd11->write(reinterpret_cast<char*>(&rep),sizeof(int));
	//Escribo las posiciones del termino
	fd11->write((char*)(&pos1),sizeof(int));
	fd11->write((char*)(&pos2),sizeof(int));

	rep = 1;
	fd11->write((char*)(&nro_doc),sizeof(int));
	fd11->write((char*)(&largoterm),1);
	fd11->write("b",largoterm);
	fd11->write(reinterpret_cast<char*>(&rep),sizeof(int));
	//Escribo las posiciones del termino
	fd11->write((char*)(&pos3),sizeof(int));
}

void TestBase::cargarArch4(){

	unsigned int nro_doc = 10;
	unsigned char largoterm = 1;
	unsigned int rep = 2;
	unsigned int pos1 = 40;
	unsigned int pos2 = 41;
	unsigned int pos3 = 42;
	unsigned int pos4 = 43;

	fd4->write((char*)(&nro_doc),sizeof(int));
	fd4->write((char*)(&largoterm),1);
	fd4->write("a",largoterm);
	fd4->write(reinterpret_cast<char*>(&rep),sizeof(int));
	//Escribo las posiciones del termino
	fd4->write((char*)(&pos1),sizeof(int));
	fd4->write((char*)(&pos2),sizeof(int));

	rep = 1;
	fd4->write((char*)(&nro_doc),sizeof(int));
	fd4->write((char*)(&largoterm),1);
	fd4->write("b",largoterm);
	fd4->write(reinterpret_cast<char*>(&rep),sizeof(int));
	//Escribo las posiciones del termino
	fd4->write((char*)(&pos3),sizeof(int));

	rep = 1;
	fd4->write((char*)(&nro_doc),sizeof(int));
	fd4->write((char*)(&largoterm),1);
	fd4->write("c",largoterm);
	fd4->write(reinterpret_cast<char*>(&rep),sizeof(int));
	//Escribo las posiciones del termino
	fd4->write((char*)(&pos4),sizeof(int));
}

void TestBase::cargarArch9(){

	unsigned int nro_doc = 13;
	unsigned char largoterm = 1;
	unsigned int rep = 3;
	unsigned int pos1 = 90;
	unsigned int pos2 = 91;
	unsigned int pos3 = 92;
	unsigned int pos4 = 93;
	unsigned int pos5 = 94;
	unsigned int pos6 = 95;
	unsigned int pos7 = 96;

	fd9->write((char*)(&nro_doc),sizeof(int));
	fd9->write((char*)(&largoterm),1);
	fd9->write("a",largoterm);
	fd9->write(reinterpret_cast<char*>(&rep),sizeof(int));
	//Escribo las posiciones del termino
	fd9->write((char*)(&pos1),sizeof(int));
	fd9->write((char*)(&pos2),sizeof(int));
	fd9->write((char*)(&pos3),sizeof(int));

	rep = 1;
	fd9->write((char*)(&nro_doc),sizeof(int));
	fd9->write((char*)(&largoterm),1);
	fd9->write("b",largoterm);
	fd9->write(reinterpret_cast<char*>(&rep),sizeof(int));
	//Escribo las posiciones del termino
	fd9->write((char*)(&pos4),sizeof(int));

	largoterm = 2;
	rep = 2;
	fd9->write((char*)(&nro_doc),sizeof(int));
	fd9->write((char*)(&largoterm),1);
	fd9->write("bb",largoterm);
	fd9->write(reinterpret_cast<char*>(&rep),sizeof(int));
	//Escribo las posiciones del termino
	fd9->write((char*)(&pos5),sizeof(int));
	fd9->write((char*)(&pos6),sizeof(int));

	rep = 1;
	fd9->write((char*)(&nro_doc),sizeof(int));
	fd9->write((char*)(&largoterm),1);
	fd9->write("ca",largoterm);
	fd9->write(reinterpret_cast<char*>(&rep),sizeof(int));
	//Escribo las posiciones del termino
	fd9->write((char*)(&pos7),sizeof(int));
}

void TestBase::cargarArch5(){

	unsigned int nro_doc = 16;
	unsigned char largoterm = 1;
	unsigned int rep = 1;
	unsigned int pos1 = 50;
	unsigned int pos2 = 51;
	unsigned int pos3 = 52;
	unsigned int pos4 = 53;
	unsigned int pos5 = 54;
	unsigned int pos6 = 55;
	unsigned int pos7 = 56;

	fd5->write((char*)(&nro_doc),sizeof(int));
	fd5->write((char*)(&largoterm),1);
	fd5->write("a",largoterm);
	fd5->write(reinterpret_cast<char*>(&rep),sizeof(int));
	//Escribo las posiciones del termino
	fd5->write((char*)(&pos1),sizeof(int));

	rep = 3;
	fd5->write((char*)(&nro_doc),sizeof(int));
	fd5->write((char*)(&largoterm),1);
	fd5->write("b",largoterm);
	fd5->write(reinterpret_cast<char*>(&rep),sizeof(int));
	//Escribo las posiciones del termino
	fd5->write((char*)(&pos2),sizeof(int));
	fd5->write((char*)(&pos3),sizeof(int));
	fd5->write((char*)(&pos4),sizeof(int));

	largoterm = 2;
	rep = 1;
	fd5->write((char*)(&nro_doc),sizeof(int));
	fd5->write((char*)(&largoterm),1);
	fd5->write("bc",largoterm);
	fd5->write(reinterpret_cast<char*>(&rep),sizeof(int));
	//Escribo las posiciones del termino
	fd5->write((char*)(&pos5),sizeof(int));

	rep = 2;
	fd5->write((char*)(&nro_doc),sizeof(int));
	fd5->write((char*)(&largoterm),1);
	fd5->write("cb",largoterm);
	fd5->write(reinterpret_cast<char*>(&rep),sizeof(int));
	//Escribo las posiciones del termino
	fd5->write((char*)(&pos6),sizeof(int));
	fd5->write((char*)(&pos7),sizeof(int));
}

void TestBase::cargarArch2(){

	unsigned int nro_doc = 55;
	unsigned char largoterm = 1;
	unsigned int rep = 3;
	unsigned int pos1 = 20;
	unsigned int pos2 = 21;
	unsigned int pos3 = 22;
	unsigned int pos4 = 23;
	unsigned int pos5 = 24;
	unsigned int pos6 = 25;
	unsigned int pos7 = 26;
	unsigned int pos8 = 27;
	unsigned int pos9 = 28;
	unsigned int pos10 = 29;

	fd2->write((char*)(&nro_doc),sizeof(int));
	fd2->write((char*)(&largoterm),1);
	fd2->write("a",largoterm);
	fd2->write(reinterpret_cast<char*>(&rep),sizeof(int));
	//Escribo las posiciones del termino
	fd2->write((char*)(&pos1),sizeof(int));
	fd2->write((char*)(&pos2),sizeof(int));
	fd2->write((char*)(&pos3),sizeof(int));

	rep = 2;
	fd2->write((char*)(&nro_doc),sizeof(int));
	fd2->write((char*)(&largoterm),1);
	fd2->write("b",largoterm);
	fd2->write(reinterpret_cast<char*>(&rep),sizeof(int));
	//Escribo las posiciones del termino
	fd2->write((char*)(&pos4),sizeof(int));
	fd2->write((char*)(&pos5),sizeof(int));

	largoterm = 2;
	rep = 2;
	fd2->write((char*)(&nro_doc),sizeof(int));
	fd2->write((char*)(&largoterm),1);
	fd2->write("cc",largoterm);
	fd2->write(reinterpret_cast<char*>(&rep),sizeof(int));
	//Escribo las posiciones del termino
	fd2->write((char*)(&pos6),sizeof(int));
	fd2->write((char*)(&pos7),sizeof(int));

	rep = 1;
	fd2->write((char*)(&nro_doc),sizeof(int));
	fd2->write((char*)(&largoterm),1);
	fd2->write("cd",largoterm);
	fd2->write(reinterpret_cast<char*>(&rep),sizeof(int));
	//Escribo las posiciones del termino
	fd2->write((char*)(&pos8),sizeof(int));

	rep = 1;
	fd2->write((char*)(&nro_doc),sizeof(int));
	fd2->write((char*)(&largoterm),1);
	fd2->write("da",largoterm);
	fd2->write(reinterpret_cast<char*>(&rep),sizeof(int));
	//Escribo las posiciones del termino
	fd2->write((char*)(&pos9),sizeof(int));

	rep = 1;
	fd2->write((char*)(&nro_doc),sizeof(int));
	fd2->write((char*)(&largoterm),1);
	fd2->write("db",largoterm);
	fd2->write(reinterpret_cast<char*>(&rep),sizeof(int));
	//Escribo las posiciones del termino
	fd2->write((char*)(&pos10),sizeof(int));
}

void TestBase::cargarArchsAMergear(){

	this->cargarArch11();
	this->cargarArch4();
	this->cargarArch9();
	this->cargarArch5();
	this->cargarArch2();
}

void TestBase::cerrarYLiberarArchs(){

	if(this->fd11->is_open())
		this->fd11->close();
	if(this->fd4->is_open())
		this->fd4->close();
	if(this->fd9->is_open())
		this->fd9->close();
	if(this->fd5->is_open())
		this->fd5->close();
	if(this->fd2->is_open())
		this->fd2->close();

	delete this->fd11;
	delete this->fd4;
	delete this->fd9;
	delete this->fd5;
	delete this->fd2;
}

void TestBase::ejecutarPruebaMerge(){

	char dirAMergear[ANCHO_DIRECTORIO_ARCH_IDX_REPO] = CARPETA_ARCHS_A_MERGEAR;
    NWayMerge miMerger = NWayMerge(dirAMergear);

	//voy llamando mis funciones paso a paso
	std::cout << "----------- PRUEBA MERGE" << std::endl;
	this->crearArchsAMergear();
	this->cargarArchsAMergear();
	this->cerrarYLiberarArchs();
	std::cout << "--------------------- Inicio merge" << std::endl;
	miMerger.mergeDeNVias();
	std::cout << "--------------------- Fin merge" << std::endl;
}

#endif /* TESTBASE_H_ */
